﻿using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
namespace AudioPlayer.GUI {
	internal class Scroller : Control {
		#region Variables
		private Color _colorBorder;
		private Color _colorBack1;
		private Color _colorBack2;

		private bool _mouseDown;
		private Point _mouseLoc;
		#endregion
		#region Constructor
		public Scroller() {
			Init();
		}
		#endregion
		#region Private Voids
		private void Init() {
			InitStyle();
			InitControl();
		}
		private void InitControl() {
			this.MinimumSize = new Size(20, 20);
			this.Size = new Size(20, 20);
		}
		private void InitStyle() {
			_colorBorder = Color.FromArgb(80, 117, 167);
			_colorBack1 = Color.FromArgb(230, 230, 230);
			_colorBack2 = Color.FromArgb(240, 240, 240);
		}
		private void DrawScroll(Graphics g) {
			if (_mouseLoc.Y > 15 && (_mouseLoc.Y < this.Height - 15)) {
				Rectangle rect = new Rectangle(0, _mouseLoc.Y, this.Width, 10);
				g.FillRectangle(Brushes.Red, rect);
			}
		}
		private void DrawArrows(Graphics g) {
			g.FillRectangle(Brushes.LightBlue, new Rectangle(0, 0, this.Width, 15));
			g.FillRectangle(Brushes.LightBlue, new Rectangle(0, this.Height - 15, this.Width, 15));
		}
		private void DrawBackground(Graphics g) {
			DrawBackground(g, this.DisplayRectangle);
		}
		private void DrawBackground(Graphics g, Rectangle rect) {
			//Douuuubbbbbllllleeeeeeeeeee Buffer
			Bitmap doubleBuff = new Bitmap(rect.Width, rect.Height);
			Graphics gr = Graphics.FromImage(doubleBuff);

			#region Background
			Rectangle rect1 = new Rectangle(rect.X, rect.Y, rect.Width / 2, rect.Height);
			Rectangle rect2 = new Rectangle(rect1.X + rect1.Width, rect.Y, rect.Width - rect1.Width, rect.Height);
			LinearGradientBrush brush1 = new LinearGradientBrush(rect1, _colorBack1, _colorBack2, LinearGradientMode.Horizontal);
			LinearGradientBrush brush2 = new LinearGradientBrush(rect2, _colorBack2, _colorBack1, LinearGradientMode.Horizontal);
			gr.FillRectangle(brush1, rect1);
			gr.FillRectangle(brush2, rect2);
			#endregion
			#region Border
			Pen pBorder = new Pen(_colorBorder);
			gr.DrawLine(pBorder, rect.X, rect.Y, rect.X,rect.Y+ rect.Height - 1);
			gr.DrawLine(pBorder, rect.X + rect.Width - 1, rect.Y, rect.X + rect.Width - 1, rect.Y + rect.Height - 1);
			#endregion
			gr.Dispose();
			g.DrawImageUnscaled(doubleBuff, rect.Location);
		}
		#endregion
		#region Override
		protected override void OnPaint(PaintEventArgs e) {
			DrawBackground(e.Graphics);
		}
		protected override void OnMouseDown(MouseEventArgs e) {
			_mouseDown = true;
			_mouseLoc = e.Location;
		}
		protected override void OnMouseUp(MouseEventArgs e) {
			_mouseDown = false;
		}
		protected override void OnMouseLeave(EventArgs e) {
			_mouseDown = false;
		}
		protected override void OnMouseMove(MouseEventArgs e) {
			if (_mouseDown) {
				_mouseLoc = e.Location;
				DrawBackground(this.CreateGraphics());
				DrawScroll(this.CreateGraphics());
				DrawArrows(this.CreateGraphics());
			}
		}
		#endregion
		#region Public Voids

		#endregion
		#region Property
		public bool IsMouseDown {
			get { return _mouseDown; }
			set { _mouseDown = value; }
		}
		#endregion
	}
}
